<?php

namespace app\components\behaviors;
use app\models\Users;
use Yii;
use yii\base\Behavior;
use yii\base\ErrorException;
use yii\bootstrap\Html;
use yii\db\ActiveRecord;

class UserBehavior extends  Behavior
{
    public function events()
    {
        return [
            ActiveRecord::EVENT_AFTER_INSERT => "addUser",
        ];
    }

    protected function saveUsers($yid,$uName,$uPhone,$quanxian)
    {
        //实例化用户类
        $users = new Users();
        //属性赋值
        $users->yey_id =$yid;
        $users->gl_username =$uName;
        $users->mobile = $uPhone;
        $users->gl_passwors = substr($uPhone,-11,6);
        $users->quanxian = $quanxian;
        //保存
        $users->save();
    }
    public function addUser(){
        //获取机构
        $school = $this->owner;
        /**@var $school \app\models\School */
        //获取幼儿园id
        $yid = $school->yey_id;

        //添加园长
        $this->saveUsers($yid,$school->yz_name,$school->yz_phone,1);
        //添加园管理员
        $this->saveUsers($yid,$school->gl_name,$school->gl_phone,2);


    }

}